//
//  WBWeicomeViewController.swift
//  Weibo
//
//  Created by 汪博文 on 2016/11/17.
//  Copyright © 2016年 Wbw. All rights reserved.
//

import UIKit
import SDWebImage

private let bottomMargin: CGFloat = 120

class WBWeicomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //设置UI界面
        setupUI()
        //动画方法在viewDidAppear中调用 不要在viewDidLoad中调用
        //根据自动布局设置的约束,不是在页面加载的时候生效,而是在viewDidLayoutSubviews才会生效
        //startAnimation()
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        //自动布局的动画效果应该在 viewDidAppear中执行才会有正确的动画效果
        startAnimation()
    }
    //开始动画
    private func startAnimation() {
        let offsetY = UIScreen.main.bounds.height - bottomMargin - iconView.bounds.height
        //更新约束
        self.iconView.snp.updateConstraints({ (make) in
            make.bottom.equalTo(self.view).offset(-offsetY)
        })
        //执行动画效果
        /*
         弹簧动画效果
         1.usingSpringWithDamping : 阻尼洗漱
         2.initialSpringVelocity : 加速度
         3.options : 动画的可选项 oc中传0 swift中传空数组
         */
        UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 7, options: [], animations: {
            //提前刷新页面 提前调用viewDidLayoutSubviews
            self.view.layoutIfNeeded()
        }, completion: { (finished) -> () in
            //动画效果完成后的回调
            UIView.animate(withDuration: 0.5, animations: { 
                self.welcomeLabel.alpha = 1
            }, completion: { (_) -> () in
                print("👌")
                //UIApplication.shared.keyWindow?.rootViewController = WBMainViewController()
                NotificationCenter.default.post(name: NSNotification.Name(kChoseRootViewController), object: nil)
            })
        })
        
    }
    private func setupUI() {
        //设置背景色
        self.view.backgroundColor = UIColor.white
        //加入头像和欢迎label
        self.view.addSubview(iconView)
        self.view.addSubview(welcomeLabel)
        //设置约束
        iconView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 85, height: 85))
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-bottomMargin)
        }
        welcomeLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp.bottom).offset(20)
        }
        //隐藏文字
        welcomeLabel.alpha = 0
        //设置头像圆角
        iconView.layer.cornerRadius = 85 * 0.5
        iconView.layer.masksToBounds = true
        //设置网络图片
//        let url = URL(string: WBUserAccountViewModel.shared.userAccount?.avatar_large ?? "")
        iconView.sd_setImage(with: WBUserAccountViewModel.shared.headURL)
    
    }
    private lazy var iconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
    private lazy var welcomeLabel: UILabel = UILabel(title: "欢迎归来", fontSize: 16, textColor: UIColor.darkGray)
}
